<?php
/**
 * Mysql Factory
 * @version 1.0.0
 * @date 2009/5/1
 * @package libs.db
 * @author konakona
 */
class mysql {
    /**
     * 数据库连接资源
     */
    private static $conn =null;
    /**
     * 连接数据库
     * @param string $dbhost 数据库主机地址
     * @param string $dbuser 用户名
     * @param string $dbpw 数据库密码
     * @param string $dbname 数据库名
     * @param string $dbcharset 数据库编码
     * @param int $pconnect 是否为持久连接
     * @param string $tablepre 表前缀
     * @time int $time 超时时间
     */
    public static function connect($dbhost = '', $dbuser = '', $dbpw = false, $dbname = '',
        $dbcharset = '', $pconnect = 0, $tablepre=false, $time = 0) {
        if(empty(self::$conn)) {
            $dbhost = ($dbhost != '') ? $dbhost : k::$config['DBHOST'];
            $dbuser = ($dbuser != '') ? $dbuser : k::$config['DBUSER'];
            $dbpw = ($dbpw !== false) ? $dbpw : k::$config['DBPASSWORD'];
            $dbname = ($dbname != '') ? $dbname : k::$config['DBNAME'];
            $dbcharset = ($dbcharset != '') ? $dbcharset :k::$config['DBCHARSET'];
            $tablepre = ($tablepre !== false) ? $tablepre : k::$config['TABLEPREFIX'];
            k::import('plugin.db');
            self::$conn = new db();
            self::$conn->connect($dbhost, $dbuser, $dbpw, $dbname, $dbcharset, $pconnect, $tablepre, $time);
        }
        return self::$conn;
    }
}
